<?php
namespace app\api\controller;

use app\common\controller\Base;
use app\common\exception\ParameterException;
use app\common\logic\Withdraw as WithdrawLogic;
use think\Loader;
use think\Request;
use think\Db;

/**
 * @title 提现模块接口
 * @description 接口说明
 * @group 接口分组
 * @header name:token require:1 default: desc:token
 * @param name:user_type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
 */

class Withdraw extends Base
{
    /**
     * @title 提现
     * @description 接口说明
     * @author WCJ
     * @url /api/withdraw/withdraw
     * @method POST
     * @param name:money type:decimal require:1 default:1 other: desc:提现金额
     * @param name:put_type type:string require:1 default:1 other: desc:提现方式1.为支付宝 2.为微信 3.为银行卡
     * @param name:pay_password type:string require:1 default:1 other: desc:密码
     * @param name:card_id type:int require:1 default:1 other: desc:卡号ID
     * @param name:type type:int require:1 default:1 other: desc:1.会员提现(满额度提现)，2.股东提现3.代理金提现4.红包提现5.商家提现6.内部提现7.一级代理提现
     *
     *
     * @return data:数据
     */
    public function withdraw(Request $request){

        $userid=$this->uid;
        $type=$this->type;
        $money=$request->post("money");
        $put_type=$request->post("put_type");
        $txtype=$request->post("type");
        $card_id =$request->post("card_id");
        $pay_password=$request->post("pay_password");
        if($type!=1&&$txtype==1) {

            throw new ParameterException(["code"=>201,"msg"=>"会员才可以提现!","errorCode"=>404]);

        }

        if($type!=1&&$txtype==2) {

            throw new ParameterException(["code"=>201,"msg"=>"会员才可以提现股东分红!","errorCode"=>404]);

        }

        if($type!=1&&$txtype==3) {

            throw new ParameterException(["code"=>201,"msg"=>"会员才可以提现代理金!","errorCode"=>404]);

        }

        if($type!=1&&$txtype==4) {

            throw new ParameterException(["code"=>201,"msg"=>"会员才可以提现红包!","errorCode"=>404]);

        }

        if($type!=2&&$txtype==5) {

            throw new ParameterException(["code"=>201,"msg"=>"商家才可以提现商家收益!","errorCode"=>404]);

        }

        if($type!=3&&$txtype==7) {

            throw new ParameterException(["code"=>201,"msg"=>"代理商才可以提现代理收益!","errorCode"=>404]);

        }

        if($type!=5&&$txtype==6) {

            throw new ParameterException(["code"=>201,"msg"=>"内部员工才可以提现平台分红收益!","errorCode"=>404]);

        }

        $validate = Loader::validate("User");

        if (!$validate->scene("withdraw")->check(['pay_password' => $pay_password,"money"=>$money,"type"=>$put_type])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new ParameterException($ret);

        }

        $withdrawLogic = new WithdrawLogic();

        $data=$withdrawLogic->userwithdraw($userid,$money,$pay_password,$put_type,$card_id,$txtype,$type);


        $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;

    }

    /**
     * @title 提现列表
     * @description 接口说明
     * @author WCJ
     * @url /api/withdraw/withdraw_list
     * @method POST
     * @param name:status type:int require:1 default:1 other: desc:1.待审核，2.提现成功，3.提现失败，4.取消
     * @param name:put_type type:string require:0 default:1 other: desc:提现方式1.为支付宝 2.为微信 3.为银行卡
     * @param name:type type:int require:1 default:1 other: desc:0.全部,1.会员提现(满额度提现)，2.股东提现3.代理金提现4.红包提现5.商家提现6.内部提现7.一级代理提现
     * @param name:page type:int require:0 default:1 other: desc:页数
     * @param name:num type:int require:0 default:1 other: desc:每页数量
     *
     *
     * @return data:数据
     */
    public function withdraw_list(Request $request){

        $userid=$this->uid;
        $type=$this->type;
        $txtype=$request->post("type");

        $put_type=$request->post("put_type");
        $status =$request->post("status")?$request->post("status"):0;
        $page=$request->post("page")?$request->post("page"):1;
        $num=$request->post("num")?$request->post("num"):20;

//        if($type!=1) {
//
//            throw new ParameterException(["code"=>201,"msg"=>"非法访问","errorCode"=>404]);
//
//        }
        $validate = Loader::validate("Common");

        if (!$validate->scene("common")->check(['id' => $put_type])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new ParameterException($ret);

        }

        if (!$validate->scene("common")->check(['id' => $status])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new ParameterException($ret);

        }


        $withdrawLogic = new WithdrawLogic();

        $data=$withdrawLogic->userwithdraw_list($type,$txtype,$userid,$put_type,$status,$page,$num);

       // return ["code"=>200,"msg"=>"成功","data"=>$data[0],"sum"=>$data[1]];

        $datas= ["code"=>200,"msg"=>"成功","data"=>$data[0],"sum"=>$data[1]];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;


    }

    /**
     * @title 提现详情
     * @description 接口说明
     * @author WCJ
     * @url /api/withdraw/withdraw_dt
     * @method POST
     * @param name:id type:int require:1 default:1 other: desc:提现id
     *
     *
     * @return data:数据
     */
    public function withdraw_dt(Request $request){

        $userid=$this->uid;
        $type=$this->type;
        $id=$request->post("id");


        $validate = Loader::validate("Common");

        if (!$validate->scene("common")->check(['id' => $id])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new ParameterException($ret);

        }


        $withdrawLogic = new WithdrawLogic();

        $data=$withdrawLogic->userwithdraw_dt($id,$userid,$type);

        $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;


    }

    /**
     * @title 提现信息
     * @description 接口说明
     * @author WCJ
     * @url /api/withdraw/withdraw_info
     * @method POST
     * @param name:put_type type:string require:1 default:1 other: desc:提现方式1.为支付宝 2.为微信 3.为银行卡
     * @param name:type type:int require:1 default:1 other: desc:1.会员提现(满额度提现)，2.股东提现3.代理金提现4.红包提现5.商家提现6.内部提现7.一级代理提现
     *
     *
     * @return data:数据
     */
    public function withdraw_info(Request $request){
        $usertype=$this->type;
        $uid=$this->uid;
        $put_type=$request->post("put_type");
        $txtype=$request->post("type");
        $put_service_bl=Db::table("t_config")->where("name","put_service")->value("data");//手续费
        $put_times=Db::table("t_config")->where("name","put_times")->value("data");//今日提现次数
        $min_put_money=Db::table("t_config")->where("name","min_put_money")->value("data");//最小提现额度
        $max_put_money=Db::table("t_config")->where("name","max_put_money")->value("data");//最大提现额度
        $data=[];
        $data['put_service_bl']=$put_service_bl;
        $data['put_times']=$put_times;
        $data['min_put_money']=$min_put_money;
        $data['max_put_money']=$max_put_money;

        if($usertype==1&& $txtype==1){
            $capitaldata = Db::table("t_capital")->where("type",2)->where("uid", $uid)->where("status", 1)->find();

            $data['put_out_money']=$capitaldata['put_out_money'];

        }elseif($usertype==1&& $txtype==2){
            $capitaldata = Db::table("t_capital")->where("type",2)->where("uid", $uid)->where("status", 1)->find();
            $data['put_out_money']=$capitaldata['bonus_money'];
        }elseif($usertype==1&& $txtype==3){
            $capitaldata = Db::table("t_capital")->where("type",2)->where("uid", $uid)->where("status", 1)->find();
            $data['put_out_money']=$capitaldata['agent_money'];
        }elseif($usertype==1&& $txtype==4){
            $capitaldata = Db::table("t_capital")->where("type",2)->where("uid", $uid)->where("status", 1)->find();
            $data['put_out_money']=$capitaldata['packet_money'];
        }elseif($usertype==2&& $txtype==5){
            $capitaldata = Db::table("t_capital")->where("type",1)->where("uid", $uid)->where("status", 1)->find();
            $data['put_out_money']=$capitaldata['put_out_money'];
        }elseif($usertype==5&& $txtype==6){
            $capitaldata = Db::table("t_real_bonus_member")->where("id", $uid)->where("status", 1)->find();
            $data['put_out_money']=$capitaldata['money'];
        }elseif($usertype==3&& $txtype==7){
            $capitaldata = Db::table("t_agent")->where("id", $uid)->where("status", 1)->find();
            $data['put_out_money']=$capitaldata['money'];
        }
        //TODO 未完
        if($put_times!=-1){

            if($usertype==1 && $usertype==3 && $usertype==5){
                if($put_times!=-1){
                    $starttime=date("Y-m-d",time());
                    $endtime=date("Y-m-d",time())." 23:59:59";
                    $count=Db::table("t_withdraw")->where("addtime","between",[strtotime($starttime),strtotime($endtime)])->where("type",$txtype)->where("usertype",$usertype)->where("uid",$uid)->count();
                    $data['put_times']=$put_times-$count;
                }

            }elseif($usertype==2){
                if($put_times!=-1){
                    $starttime=date("Y-m-d",time());
                    $endtime=date("Y-m-d",time())." 23:59:59";
                    $count=Db::table("t_shop_capital_put")->where("created_at","between",[$starttime,$endtime])->where("shop_id",$uid)->count();
                    $data['put_times']=$put_times-$count;
                }

            }

        }

        $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;
    }

}